package custom_types;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Rating {
	@Column
	double rating;
	@Column
	int count;
	
	public Rating(){
		this.rating = 0.0;
		this.count = 0;
	}
	
	//Getters
	public double getRating() { return rating; }

	//Setters
	public void setRatingValue(double rating) { this.rating = rating;}
	
	//Validation function
	public boolean isValid(double rating){
		return (rating >= 0.0 && rating <= 10.0) ? true:false;
	}
	
	//Add new rating
	public void addRating(double rating){
		if (isValid(rating)){
			if (count == 0)
				this.rating = rating;
			else
				this.rating = (this.rating * count + rating) / ((double) count+1);
			count++;
		}
	}
	
	
}
